require "net/http"  
module JamendoMethod
  
  #method to download the id of an artist's album
  #alias http_id
  def JamendoMethod::get_id(artist, id)
    home = `echo $HOME`.chomp
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29").gsub("+", "%2B")
    system("wget -O $HOME/.jamendo/#{artist_cor.chop}/id_alb http://www.jamendo.com/get/album/name/artist/id/plain/#{artist_cor.chop}/?n=#{id}" )
    puts "debug!"
    list = File.new "#{home}/.jamendo/#{artist_cor.chop}/id_alb"
    list_id = list.readlines
    lid = list_id[-1]
    art_id_current = File.new("#{home}/.jamendo/#{artist_cor.chop}/id_art_current", "w+")
    art_id_current.puts("#{lid}")
    art_id_current.close
    list.close
  end
  #metodo per scaricare gli album dell'artista
  def JamendoMethod::album(artist, label1, label2)
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29").gsub("+", "%2B")
    home = `echo $HOME`.chomp
    system("mkdir $HOME/.jamendo")
    system("mkdir $HOME/jamendo-stuff")
    sleep 0.1
    system("mkdir $HOME/.jamendo/#{artist_cor.chop}")
    system("mkdir $HOME/jamendo-stuff/#{artist_cor.chop}")
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29")
    system("wget -O $HOME/.jamendo/list http://www.jamendo.com/get/album/list/artist/title/plain/?names=#{artist_cor}")
    album = File.new "#{home}/.jamendo/list"
    album_read = album.readlines
    label1.set_text("There's the artist's album")
    label2.set_text(album_read.join)
    album.close
  end
  #metodo per scaricare la playlist in formato plain
  def JamendoMethod::playlist(artist, label1, label2)
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29").gsub("+", "%2B")
    home = `echo $HOME`.chomp
    file_id = File.new "#{home}/.jamendo/#{artist_cor.chop}/id_art_current"
    album_id = file_id.readline
    system("wget -O /home/$USER/jamendo-stuff/#{artist_cor.chop}/#{artist_cor.chop}-#{album_id.chop}-playlist.txt http://www.jamendo.com/get/track/list/album/title/plain/?ids=#{album_id.chop}")
    file_play = File.new "#{home}/jamendo-stuff/#{artist_cor.chop}/#{artist_cor.chop}-#{album_id.chop}-playlist.txt"
    play_read = file_play.readlines
    label1.set_text("There is the albums playlist:")
    label2.set_text(play_read.join)
  end
  #metodo per scaricare liste m3u
  def JamendoMethod::m3u(artist)
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29").gsub("+", "%2B")
    home = `echo $HOME`.chomp
    file_id = File.new "#{home}/.jamendo/#{artist_cor.chop}/id_art_current"
    album_id = file_id.readline
    system( "wget -O /home/$USER/jamendo-stuff/#{artist_cor.chop}/#{artist_cor.chop}-#{album_id.chop}.m3u  http://www.jamendo.com/get/track/list/album/audio/m3u/?ids=#{album_id.chop}")
  end
  #metodo per scaricare copertine
  def JamendoMethod::artwork(artist, artspace)
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29").gsub("+", "%2B")
    home = `echo $HOME`.chop
    file_id = File.new "#{home}/.jamendo/#{artist_cor.chop}/id_art_current"
    album_id = file_id.readline
    system ("wget -O $HOME/jamendo-stuff/#{artist_cor.chop}/#{artist_cor.chop}-image#{album_id.chomp}.jpg http://www.jamendo.com/get/album/list/album/artworkurl/redirect/?ids=#{album_id.chomp}")    
    artspace.set_file("#{home}/jamendo-stuff/#{artist_cor.chop}/#{artist_cor.chop}-image#{album_id.chomp}.jpg")
  end
  
  #metodo per scaricare torrent
  def JamendoMethod::torrent(artist)
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29").gsub("+", "%2B")
    home = `echo $HOME`.chop
    file_id = File.new "#{home}/.jamendo/#{artist_cor.chop}/id_art_current"
    album_id = file_id.readline
    system("wget -O $HOME/jamendo-stuff/#{artist_cor.chop}/#{artist_cor.chop}-#{album_id.chop}.torrent  http://www.jamendo.com/get/album/list/album/p2p/redirect/?ids=#{album_id.chop}")
  end
  #fa partire il player  
  def JamendoMethod::player(player,artist)
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29").gsub("+", "%2B")
    home = `echo $HOME`.chop
    file_id = File.new "#{home}/.jamendo/#{artist_cor.chop}/id_art_current"
    album_id = file_id.readline
    system("#{player} $HOME/jamendo-stuff/#{artist_cor.chop}/#{artist_cor.chop}-#{album_id.chop}.m3u")
  end
  
  #scarica le canzoni via streaming
  def JamendoMethod::dumpstream(artist, num, label1, window)
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29").gsub("+", "%2B")
    home = `echo $HOME`.chop
    list = File.new "#{home}/.jamendo/#{artist_cor.chop}/id_alb"
    list_id = list.readlines
    system("wget -O ~/.jamendo/#{artist_cor.chop}/stream.txt http://www.jamendo.com/get/track/id/album/audio/plain/#{list_id.to_s}/?n=#{num}")
    stream = File.new "#{home}/.jamendo/#{artist_cor.chop}/stream.txt"
    stream_r = stream.readlines
    Thread.new do
      system("mplayer  #{stream_r[-1].to_s} -dumpstream  -dumpfile ~/jamendo-stuff/#{artist_cor.chop}/song-#{num.to_s}.mp3")
      puts "finished"
      window.show_all
      label1.set_text("Finito download del file song-#{num.to_s}.mp3 nella cartella jamendo-stuff")
    end
  end
  
#prende informazioni sul genere della musica dell'artista
  def JamendoMethod::get_info(artist, label)
    artist_cor = artist.gsub("(", "%28").gsub(")", "%29").gsub("+", "%2B")
    home = `echo $HOME`.chop
    list = File.new "#{home}/.jamendo/#{artist_cor.chop}/id_alb"
    list_id = list.readlines
    name =  Net::HTTP.get 'www.jamendo.com', "http://www.jamendo.com/get/album/list/album/data/json/?ali=info_min&n=1&ali=genre&ids=#{list_id}"        
    #puts "leggo"
    #info_file = File.new "#{home}/.jamendo/#{artist_cor.chop}/info"
    #info_read = info_file.readlines
    inf =  name.split(",")
    label.set_text("Genre: #{inf[0].gsub('[{"genre_str":', '').gsub('"', "")}")
  end
end
